package com.common.project.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/***
 * @Title
 * @Author jiangshicun@bicai365.com
 * @Description TODO
 * @Date 13:21 2019/7/1
 * @Param
 * @return
 */
public  class  DateUtil{
    private static final Logger logger = LoggerFactory.getLogger(DateUtil.class);

    // 默认格式
    public static final String ddMMyy = "dd/MM/yy";
    // 长日期格式
    public static final String yyyyMMdd = "yyyyMMdd";
    // 短日期格式
    public static final String yyMMdd = "yy-MM-dd";
    // 短日期格式
    public static final String MMdd = "MM-dd";
    // 短日期格式
    public static final String yyyyMM = "yyyy-MM";
    // 长日期格式
    public static final String yyyy_MM_dd = "yyyy-MM-dd";
    // 时间格式
    public static final String HHmmss = "HH:mm:ss";

    public static final String HHmm = "HH:mm";

    // 长日期时间格式
    public static final String yyyyMMddHHmmss = "yyyy/MM/dd HH:mm:ss";
    // 短日期时间格式
    public static final String yyMMddHHmmss = "yy-MM-dd HH:mm:ss";
    public static final String yyyy_MM_ddHHmmss = "yyyy-MM-dd HH:mm:ss";
    public static final String yyyy_MM_ddHHmm = "yyyy-MM-dd HH:mm";

    public static final String yyyy_MM_dd_HH_mm = "yyyy-MM-dd-HH:mm";

    public static final String[] WEEK_NAME = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
    public static final String[] WEEK_NAME_SIMPLE = {"日", "一", "二", "三", "四", "五", "六"};

    public static Date string2Date(String dateString, String model) {
        Date date = null;
        SimpleDateFormat sdf = new SimpleDateFormat(model);
        try {
            date = sdf.parse(dateString);
        } catch (ParseException e) {
            logger.error("DateUtil.String2Date error!");
            e.printStackTrace();
        }
        return date;
    }

    public static Date nDaysAfterNowDate(int n) {
        Calendar rightNow = Calendar.getInstance();
        rightNow.add(5, n);
        return rightNow.getTime();
    }

    public static Date getFormatDate(Date date, String model) {
        SimpleDateFormat sdf = new SimpleDateFormat(model);
        Date formatdate = null;
        try {
            formatdate = sdf.parse(sdf.format(date));
        } catch (ParseException e) {
            logger.error("日期转换失败", e);
        }
        return formatdate;
    }

    public static String Date2String(Date date, String model) {
        SimpleDateFormat sdf = new SimpleDateFormat(model);
        String strdate = sdf.format(date);
        return strdate;
    }

    //获取当前时间是一年中的第几天
    public static int getYearOfDay() {
        Date date = new Date();
        Calendar ca = Calendar.getInstance();
        ca.setTime(date);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
        String st = sdf.format(date);
        int i = ca.get(Calendar.DAY_OF_YEAR);
        return i;
    }

    //获取当前时间是一个月中的第几天
    public static int getMonthOfDay() {
        Date date = new Date();
        Calendar ca = Calendar.getInstance();
        ca.setTime(date);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
        String st = sdf.format(date);
        int a = ca.get(Calendar.DAY_OF_MONTH);
        return a;
    }

    //获取当前时间中的年
    public static int getCurrentYear() {
        Date date = new Date();
        /*SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日");
        String st=sdf.format(date);
        String yearStr=st.substring(0,4);
        int year=Integer.parseInt(yearStr);*/
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH) + 1;
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        return year;
    }

    //获取当前时间中的年
    public static int getCurrentYear(Date date) {
        /*SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日");
        String st=sdf.format(date);
        String yearStr=st.substring(0,4);
        int year=Integer.parseInt(yearStr);*/
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH) + 1;
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        return year;
    }

    public static Map<String, Integer> getCurrentYearAndMonth() {
        HashMap<String, Integer> map = new HashMap<>();
        map.put("year", getCurrentYear());
        map.put("month", getCurrentMonth());
        return map;
    }


    //获取当前时间中的月
    public static int getCurrentMonth() {
        Date date = new Date();
       /* SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日");
        String st=sdf.format(date);
        String monthStr=st.substring(5,7);
        int month=Integer.parseInt(monthStr);*/
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        // int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH) + 1;
        //   int day=calendar.get(Calendar.DAY_OF_MONTH);

        return month;
    }

    //获取当前时间中的月
    public static int getCurrentMonth(Date date) {
       /* SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日");
        String st=sdf.format(date);
        String monthStr=st.substring(5,7);
        int month=Integer.parseInt(monthStr);*/
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        // int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH) + 1;
        //   int day=calendar.get(Calendar.DAY_OF_MONTH);

        return month;
    }

    //获取当前时间中的天
    public static int getCurrentDay() {
        Date date = new Date();
        /*SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日");
        String st=sdf.format(date);
        String dayStr=st.substring(8,10);
        int day=Integer.parseInt(dayStr);*/
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH) + 1;
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        return day;
    }

    //获取当前时间中的天
    public static int getCurrentDay(Date date) {
        /*SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日");
        String st=sdf.format(date);
        String dayStr=st.substring(8,10);
        int day=Integer.parseInt(dayStr);*/
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH) + 1;
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        return day;
    }

    public static int compare_date(Date startDate, Date endDate) {

        if (startDate.getTime() > endDate.getTime()) {
            return 1;
        } else if (startDate.getTime() < endDate.getTime()) {
            return -1;
        } else {
            return 0;
        }

    }        //获取当前时间中的小时

    public static int getCurrentHourOfDay(Date date) {
        /*SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日");
        String st=sdf.format(date);
        String dayStr=st.substring(8,10);
        int day=Integer.parseInt(dayStr);*/
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        // int year = calendar.get(Calendar.YEAR);
        //int month=calendar.get(Calendar.MONTH)+1;
        //int day=calendar.get(Calendar.DAY_OF_MONTH);
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        return hour;
    }

    //获取当前时间中的小时
    public static int getCurrentHourOfDay() {
        Date date = new Date();
        /*SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日");
        String st=sdf.format(date);
        String dayStr=st.substring(8,10);
        int day=Integer.parseInt(dayStr);*/
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        // int year = calendar.get(Calendar.YEAR);
        //int month=calendar.get(Calendar.MONTH)+1;
        //int day=calendar.get(Calendar.DAY_OF_MONTH);
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        return hour;
    }


    //获取当前时间中的分钟
    public static int getCurrentMinuteOFHour(Date date) {
        /*SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日");
        String st=sdf.format(date);
        String dayStr=st.substring(8,10);
        int day=Integer.parseInt(dayStr);*/
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        // int year = calendar.get(Calendar.YEAR);
        //int month=calendar.get(Calendar.MONTH)+1;
        //int day=calendar.get(Calendar.DAY_OF_MONTH);
        int minute = calendar.get(Calendar.MINUTE);
        return minute;
    }

    //获取当前时间中的分钟
    public static int getCurrentMinuteOFHour() {
        Date date = new Date();
        /*SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日");
        String st=sdf.format(date);
        String dayStr=st.substring(8,10);
        int day=Integer.parseInt(dayStr);*/
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        // int year = calendar.get(Calendar.YEAR);
        //int month=calendar.get(Calendar.MONTH)+1;
        //int day=calendar.get(Calendar.DAY_OF_MONTH);
        int minute = calendar.get(Calendar.MINUTE);
        return minute;
    }


    /**
     * 获得某个月最大天数
     *
     * @param year  年份
     * @param month 月份 (1-12)
     * @return 某个月最大天数
     */
    public static int getMaxDayByYearMonth(int year, int month) {
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        Date parse = null;
        try {
            parse = sdf.parse(year + "-" + month);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        calendar.setTime(parse);
        return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);

    }


    /**
     * 获得某个月最大天数
     *
     * @return 某个月最大天数
     */
    public static String getMaxDayByYearMonth(String date1) {
        String[] split = date1.split("-");
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, Integer.parseInt(split[0]));
        calendar.set(Calendar.MONTH, Integer.parseInt(split[1]) - 1);
        return calendar.getActualMaximum(Calendar.DATE) + "";
    }

    /**
     * 字符串转化成日期时间
     *
     * @param s
     * @param style
     * @return
     */
    public static Date parseToDate(String s, String style) {
        // 实例化日期格式化类s
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat();
        simpleDateFormat.applyPattern(style);
        Date date = null;
        // 字符串为空或长度小于8
        if (s == null)
            return null;
        try {
            date = simpleDateFormat.parse(s);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    public static Date parseToDate(Date d, String style) {
        // 实例化日期格式化类s
        SimpleDateFormat format = new SimpleDateFormat(style);
        String dd = format.format(d);
        Date date = null;
        try {
            date = format.parse(dd);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    public static String parseToString(Date d, String style) {
        // 实例化日期格式化类s
        SimpleDateFormat format = new SimpleDateFormat(style);
        String dd;
        try {
            dd = format.format(d);
            return dd;
        } catch (RuntimeException e) {
            return null;
        }
    }

    public static String parseToString(String d, String style) {
        // 实例化日期格式化类s
        SimpleDateFormat format = new SimpleDateFormat(style);
        String dd;
        try {
            dd = format.format(d);
            return dd;
        } catch (RuntimeException e) {
            return null;
        }
    }

    /**
     * 日期++
     *
     * @param date
     * @param n
     * @return
     */
    public static Date addDate(Date date, int n) {
        try {
            Calendar cd = Calendar.getInstance();
            cd.setTime(date);
            cd.add(Calendar.DATE, n);// 增加一天
            return cd.getTime();
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 日期++
     *
     * @param date
     * @param n
     * @return
     */
    public static Date addDate(Date date, int type, int n) {
        try {
            Calendar cd = Calendar.getInstance();
            cd.setTime(date);
            cd.add(type, n);// 增加一天
            return cd.getTime();
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 日期++
     *
     * @param date
     * @param n
     * @param style
     * @return
     */
    public static String addDate(String date, int n, String style) {
        try {
            Date d = parseToDate(date, style);
            Calendar cd = Calendar.getInstance();
            cd.setTime(d);
            cd.add(Calendar.DATE, n);// 增加一天
            return parseToString(cd.getTime(), style);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 日期++
     *
     * @param date
     * @param n
     * @param style
     * @return
     */
    public static String addDate(Date date, int type, int n, String style) {
        try {
            Date d = parseToDate(date, style);
            Calendar cd = Calendar.getInstance();
            cd.setTime(d);
            cd.add(type, n);
            return parseToString(cd.getTime(), style);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 给定月份算出有多少天
     *
     * @param date
     * @return
     */
    public static int getMaxDay(Date date) {
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        int maxDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
        return maxDay;
    }


    public static List<String> GetDatesFromRange(String startDateStr, String endDateStr) {
        try {
            Date startDate = new SimpleDateFormat("yyyy-MM-dd").parse(startDateStr);//定义起始日期
            Date endDate = new SimpleDateFormat("yyyy-MM-dd").parse(endDateStr);//定义结束日期
            Calendar calendar = Calendar.getInstance();//定义日期实例
            calendar.setTime(startDate);//设置日期起始时间
            List<String> dates = new ArrayList<String>();
            while (compareDate(calendar.getTime(), endDate)) {//判断是否到结束日期
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                String str = sdf.format(calendar.getTime());
                dates.add(str);
                calendar.add(Calendar.DAY_OF_MONTH, 1);//进行当前日期月份加1
            }
            return dates;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }

    public static String getCurrentDate() {
        return getCurrentYear() + "-" + getCurrentMonth() + "-" + getCurrentDay();
    }


    public static boolean compareDate(Date start, Date end) {
        try {
            return !(end.compareTo(start) < 0);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }

    }

    /**
     * 给定日期算出有多少天
     *
     * @param sDate
     * @param eDate
     * @return
     */
    public static long getIntervalDay(Date sDate, Date eDate) {
        long s = sDate.getTime();
        long e = eDate.getTime();
        long intervalDay = (s - e) / (1000 * 60 * 60 * 24);
        return intervalDay + 1;
    }

    /**
     * 返回当前日期
     *
     * @param style
     * @return
     */
    public static String todayToString(String style) {
        // 实例化日期格式化类s
        SimpleDateFormat format = new SimpleDateFormat(style);
        String dd;
        try {
            dd = format.format(new Date());
            return dd;
        } catch (RuntimeException e) {
            return null;
        }
    }

    //返回的是字符串型的时间，输入的
    public static Date addDateMinut(Date date, int minute) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.MINUTE, minute);// 24小时制
        date = cal.getTime();
        return date;
    }

    /**
     * 日期转换为XMLGregorianCalendar
     *
     * @param date
     * @return
     */
    public static XMLGregorianCalendar getXMLGregorianCalendar(Date date) {
        XMLGregorianCalendar xgcal = null;
        try {
            GregorianCalendar gcal = new GregorianCalendar();
            gcal.setTime(date);
            xgcal = DatatypeFactory.newInstance().newXMLGregorianCalendar(gcal);
        } catch (DatatypeConfigurationException e) {
        }
        return xgcal;
    }

    /**
     * 日期XMLGregorianCalendar转换为string
     */
    public static String XMLGregorianCalendartoString(String date) {
        String d = null;
        try {
            d = date.substring(0, 10) + " " + date.substring(11, 19);
        } catch (Exception e) {
            d = null;
        }
        return d;
    }

    /**
     * 日期比较大小
     *
     * @return
     */
    public static boolean compareTheSize(Date date, Date startDate, Date endDate) {
        return startDate.getTime() <= date.getTime()
                && date.getTime() <= endDate.getTime();

    }

    /**
     * @return
     * @Title: getMonthDays
     * @Description: 获取两个日期之间的天数
     */

    public static int getBetweenListDays(String date, String date1) {

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date startDate = null;
        try {
            startDate = format.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Calendar startTime = Calendar.getInstance();
        startTime.clear();
        startTime.setTime(startDate);
        int startYear = startTime.get(Calendar.YEAR);
        int startMonth = startTime.get(Calendar.MONTH);
        int startDay = startTime.get(Calendar.DAY_OF_MONTH);
        Date endDate = null;
        try {
            endDate = format.parse(date1);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Calendar endTime = Calendar.getInstance();
        endTime.clear();
        endTime.setTime(endDate);
        int endYear = endTime.get(Calendar.YEAR);
        int endMonth = endTime.get(Calendar.MONTH);
        int endDay = endTime.get(Calendar.DAY_OF_MONTH);

        int count = 0;
        List<Integer> listdays = new ArrayList<Integer>();
        for (int x = startYear; x <= endYear; x++) {
            // 罗马历法产生年份公元1582年
            int gregorianCutoverYear = 1582;
            boolean isLeapYear = x >= gregorianCutoverYear ? ((x % 4 == 0) && ((x % 100 != 0) || (x % 400 == 0)))
                    : (x % 4 == 0);
            // 判断是否是闰年
            // java方法
            // boolean isLeapYear = (new GregorianCalendar()).isLeapYear(x);

			/*
             * String isBigYear = "是平年"; if (isLeapYear) { isBigYear = "是闰年"; }
			 * System.out.println(x + "年" + isBigYear);
			 */
            // 获取开始月的最大天数；大月是1，3，5，7，8，10，12；小月是4，6，9，11；特殊月是2
            int max = 0;
            if (startMonth == 1) {
                if (isLeapYear) {
                    max = 29;
                }
                if (!isLeapYear) {
                    max = 28;
                }
            }
            if (startMonth == 3 || startMonth == 5 || startMonth == 8 || startMonth == 10) {
                max = 30;
            }
            if (startMonth == 0 || startMonth == 2 || startMonth == 4 || startMonth == 6 || startMonth == 7
                    || startMonth == 9 || startMonth == 11) {
                max = 31;
            }

            // 循环每个月
            // 如果在日期范围内月份循环时自增到了一年的最后一个月就将月份初始化到一月份
            int y = 0;
            // 如果是开始日期的第一个年的月数就从开始月数循环
            if (x == startYear) {
                y = startMonth;
            }
            for (; y < 12; y++) {

                // 获取当月的最大天数；大月是1，3，5，7，8，10，12；小月是4，6，9，11；特殊月是2
                max = 0;
                if (y == 1) {
                    if (isLeapYear) {
                        max = 29;
                    }
                    if (!isLeapYear) {
                        max = 28;
                    }
                }
                if (y == 3 || y == 5 || y == 8 || y == 10) {
                    max = 30;
                }
                if (y == 0 || y == 2 || y == 4 || y == 6 || y == 7 || y == 9 || y == 11) {
                    max = 31;
                }

                // System.out.println(x + "年" + ty + "月");

                // 循环每一天
                int z = 1;
                // 如果是开始日期的第一个月的天数就从开始天数循环
                if (x == startYear && y == startMonth) {
                    z = startDay;
                }
                for (; z <= max; z++) {
                    count++;

                    // System.out.println(x + "年" + ty + "月" + z + "日");

                    listdays.add(z);
                    if (x == endYear && y == endMonth && z == endDay) {
                        break;
                    }
                }

                // 如果已经遍历过了截至日期的最后月份就中止月份的循环
                if (x == endYear && y == endMonth) {
                    break;
                }

            }
        }

        // System.out.println(date + " 到 " + date1 + " 的天数差：" + count);
        return count - 1;
    }

    /**
     * @param dateStart
     * @param dateEnd
     * @return
     * @Title: diffDate
     * @Description: 两个日期相差几天
     */
    public static int diffDate(Date dateStart, Date dateEnd) {
        return (int) ((getMillis(dateStart) - getMillis(dateEnd)) / (24 * 3600 * 1000));
    }

    /**
     * @param dateStart
     * @param dateEnd
     * @return
     * @Title: diffDate
     * @Description: 两个日期相差几年
     */
    public static int diffDateYear(Date dateStart, Date dateEnd) {
        return (int) ((getMillis(dateStart) - getMillis(dateEnd)) / (24 * 3600 * 1000))/365;
    }
    /**
     * @param dateStart
     * @param dateEnd
     * @return
     * @Title: diffDate
     * @Description: 两个日期相差秒数
     */
    public static int diffDateSecond(Date dateStart, Date dateEnd) {
        return (int) ((getMillis(dateStart) - getMillis(dateEnd)) / 1000);
    }

    /**
     * @param dateStart
     * @param dateEnd
     * @return
     * @Title: diffDate
     * @Description: 两个日期相差分钟数
     */
    public static int diffDateMinutes(Date dateStart, Date dateEnd) {
        return (int) ((getMillis(dateStart) - getMillis(dateEnd)) / (1000 * 60));
    }

    /**
     * @param dateStart
     * @param dateEnd
     * @return
     * @Title: diffDate
     * @Description: 两个日期相差小时数
     */
    public static int diffDateHour(Date dateStart, Date dateEnd) {
        return (int) ((getMillis(dateStart) - getMillis(dateEnd)) / (3600 * 1000));
    }

    /**
     * @param date
     * @return
     * @Title: getMillis
     * @Description: 返回当前毫秒
     */
    public static long getMillis(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        return c.getTimeInMillis();
    }


    public static String getTimeBySecond(int second) {

        return null;
    }

    /**
     * 秒转化成时间
     *
     * @param time
     * @return
     */
    public static String secToTime(int time) {
        String timeStr = null;
        int hour = 0;
        int minute = 0;
        int second = 0;
        if (time <= 0)
            return "0分钟";
        else {
            minute = time / 60;
            if (minute < 60) {
                second = time % 60;
                if (second != 0 && second >= 30) {
                    minute++;
                }
                if (minute == 60) {
                    timeStr = "1小时";
                } else {
                    timeStr = minute + "分钟";
                }
            } else {
                hour = minute / 60;
                if (hour > 99)
                    return "24小时";
                minute = minute % 60;
                second = time - hour * 3600 - minute * 60;
                if (second != 0 && second >= 30) {
                    minute++;
                }
                if (minute == 60) {
                    hour++;
                    timeStr = hour + "小时";
                } else if (minute == -1) {
                    hour--;
                    if (hour == 0) {
                        timeStr = 59 + "分钟";
                    } else {
                        timeStr = hour + "小时" + 59 + "分钟";
                    }
                } else {
                    timeStr = hour + "小时" + minute + "分钟";
                }
            }
        }
        return timeStr;
    }

    /**
     * 计算当前天数是第几周
     */
    public static String calculateWeekByString(String date, String style) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(parseToDate(date, style));
        return String.valueOf(calendar.get(Calendar.WEEK_OF_YEAR));
    }

    /**
     * 计算当月最大天数
     */
    public static String calculateMonthDayNumByString(String date, String style) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(parseToDate(date, style));
        return String.valueOf(calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
    }

    /**
     * 根据给定日期获取周几
     */
    public static int calculateWhichDayOfWeek(String date, String style) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(parseToDate(date, style));
        return calendar.get(Calendar.DAY_OF_WEEK) - 1;
    }

    /**
     * 按照yyyy-MM-dd格式获取当前日期
     *
     * @return
     */
    public static Date getDate() {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        try {
            return dateFormat.parse(dateFormat.format(new Date()));
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 按照yyyy-MM-dd HH:mm:ss的格式获取系统当前时间
     */
    public static Date getTime() {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            return dateFormat.parse(dateFormat.format(new Date()));
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 按照yyyy-MM-dd HH:mm:ss的格式获取系统当前时间
     *
     * @param date
     */
    public static Date getTime(String date) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            return dateFormat.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 按照指定格式将字符串转换为日期
     *
     * @param format
     * @param source
     */
    public static Date getDate(String format, String source) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(format);
        try {
            if (StringTools.isNotEmpty(source)) {
                return dateFormat.parse(source);
            } else {
                return null;
            }
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 将yyyy-MM-dd格式的字符串转换为日期类型
     *
     * @param source
     * @return
     */
    public static Date getDate(String source) {
        if (source != null) {
            return getDate("yyyy-MM-dd", source);
        } else {
            return null;
        }
    }

    /**
     * 将日期转换为字符串类型
     *
     * @return
     */
    public static String date2Char(Date date) {
        try {
            if (date != null) {
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
                return format.format(date);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 将日期转换为字符串类型的时间
     *
     * @return
     */
    public static String time2Char(Date date) {
        try {
            if (date != null) {
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                return format.format(date);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 将日期转换为字符串类型的时间
     *
     * @return
     */
    public static String time2Char2(Date date) {
        try {
            if (date != null) {
                SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日HH点mm分");
                return format.format(date);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取两个日期相差的天数
     *
     * @param start 起始日期
     * @param end   截止日期
     * @return
     */
    public static Integer getDays(Date start, Date end) {
        try {
            if (start == null || end == null) {
                return null;
            }
            int days = (int) ((end.getTime() - start.getTime()) / (60000 * 60 * 24));
            return days;
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("get days is error");
        }
    }

    /**
     * 获取日期之间相隔几个月
     *
     * @param start
     * @param end
     * @return
     */
    public static int getBetweenMonths(Date start, Date end) {
        //第一步看相隔多少年
        Calendar startTime = Calendar.getInstance();
        startTime.setTime(start);
        Calendar endTime = Calendar.getInstance();
        endTime.setTime(end);
        int startYear = startTime.get(Calendar.YEAR);
        int endYear = endTime.get(Calendar.YEAR);
        int startMonth = startTime.get(Calendar.MONTH);
        int endMonth = endTime.get(Calendar.MONTH);
        int startDay = startTime.get(Calendar.DATE);
        int endDay = endTime.get(Calendar.DATE);
        if (end.getTime() < start.getTime()) {
            return 0;
        } else {
            int n = (endYear - startYear) * 12 + (endMonth - startMonth);
            n = endDay < startDay ? (n - 1) : n;
            return n;
        }
    }

    /**
     * 根据日期或之间的月份
     *
     * @param minDate
     * @param maxDate
     * @return
     * @throws ParseException
     */
    public static List<String> getMonthBetween(String minDate, String maxDate) throws ParseException {
        ArrayList<String> result = new ArrayList<String>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");//格式化为年月

        Calendar min = Calendar.getInstance();
        Calendar max = Calendar.getInstance();

        min.setTime(sdf.parse(minDate));
        min.set(min.get(Calendar.YEAR), min.get(Calendar.MONTH), 1);

        max.setTime(sdf.parse(maxDate));
        max.set(max.get(Calendar.YEAR), max.get(Calendar.MONTH), 2);

        Calendar curr = min;
        while (curr.before(max)) {
            result.add(sdf.format(curr.getTime()));
            curr.add(Calendar.MONTH, 1);
        }

        return result;
    }




    /**
     * 获取日期间除去正月后，还剩多少天
     *
     * @param start
     * @param end
     * @return
     */
    public static long getBetweenDaysOffMonth(Date start, Date end) {
        int betweenMonth = getBetweenMonths(start, end);
        Calendar startTime = Calendar.getInstance();
        startTime.setTime(start);
        startTime.add(Calendar.MONTH, betweenMonth);
        long n = getOverDayNum(startTime.getTime(), end);
        return n < 0 ? 0 : n;
    }

    /**
     * 获取两个日期相差的天数
     *
     * @param start 起始日期
     * @param end   截止日期
     * @return
     */
    public static Integer getBetweenHours(Date start, Date end) {
        try {
            if (start == null || end == null) {
                return 0;
            }
            if (end.getTime() <= start.getTime()) {
                return 0;
            }
            int hours = (int) ((end.getTime() - start.getTime()) / (1000 * 60 * 60));
            return hours;
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("getBetweenHours is error");
        }
    }

    /**
     * 按照指定的格式返回当前日期的字符串表是形式
     *
     * @param format
     * @return
     */
    public static String getDateForChar(String format) {
        try {
            SimpleDateFormat dateFormat = new SimpleDateFormat(format);
            return dateFormat.format(new Date());
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 按照yyyy-MM-dd格式返回当前日期的字符串表是形式
     *
     * @return
     */
    public static String getDateForChar() {
        return getDateForChar("yyyy-MM-dd");
    }

    public static Date addHours(Date source, int hours) {
        long sourceTime = source.getTime();
        long endTime = sourceTime + hours * 3600 * 1000;
        Date end = new Date(endTime);
        return end;
    }

    /**
     * 为原日期添加指定的天数并返回添加后的日期，如果天数为负数则在原日期的基础上减去指定的天数
     *
     * @param source
     * @param days
     * @return
     */
    public static Date addDays(Date source, int days) {
        try {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(source);
            calendar.add(Calendar.DAY_OF_YEAR, days);
            return calendar.getTime();
        } catch (Exception e) {
            throw new RuntimeException("add days is error.");
        }
    }

    /**
     * 为原日期添加指定的年数并返回添加后的日期，如果年数为负数则在原日期的基础上减去指定的年数
     *
     * @param source
     * @param year
     * @return
     */
    public static Date addYear(Date source, int year) {
        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
            format.parse(format.format(source));
            Calendar calendar = format.getCalendar();
            calendar.add(Calendar.YEAR, year);
            return calendar.getTime();
        } catch (Exception e) {
            throw new RuntimeException("add days is error.");
        }
    }

    /**
     * 取出剩余天数
     *
     * @param startDate 开始时间
     * @param overDate  结束时间
     * @return day
     */
    public static long getOverDayNum(Date startDate, Date overDate) {
        long day = 0;
        day = overDate.getTime() - startDate.getTime();
        day = day / 1000 / 60 / 60 / 24;

        return day < 0 ? 0 : day;
    }

    /**
     * 取出当前年
     *
     * @return
     */
    public static String getYear() {
        Calendar cal = Calendar.getInstance();
        Integer year = cal.get(Calendar.YEAR);
        return year.toString();
    }

    /**
     * 取出当前月
     *
     * @return
     */
    public static String getMonth() {
        Calendar cal = Calendar.getInstance();
        Integer month = cal.get(Calendar.MONTH) + 1;
        return month < 10 ? "0" + month : month.toString();
    }

    /**
     * 取出当前日
     *
     * @return
     */
    public static String getDayOfMonth() {
        Calendar cal = Calendar.getInstance();
        Integer day = cal.get(Calendar.DAY_OF_MONTH);
        return day < 10 ? "0" + day : day.toString();
    }

    /**
     * 取得指定日期为星期几
     *
     * @param date
     * @return 返回1是星期日、2是星期一、3是星期二、4是星期三、5是星期四、6是星期五、7是星期六
     */
    public static int getDayofWeek(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return cal.get(Calendar.DAY_OF_WEEK);
    }

    /**
     * 取得指定日期为星期几
     *
     * @param date
     * @return 返回1是星期日、2是星期一、3是星期二、4是星期三、5是星期四、6是星期五、7是星期六
     */
    public static String getDayofWeekName(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return WEEK_NAME[cal.get(Calendar.DAY_OF_WEEK) - 1];
    }

    /**
     * 取得指定日期为星期几
     *
     * @param date
     * @return 返回1是星期日、2是星期一、3是星期二、4是星期三、5是星期四、6是星期五、7是星期六
     */
    public static String getDayofWeekSimpleName(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return WEEK_NAME_SIMPLE[cal.get(Calendar.DAY_OF_WEEK) - 1];
    }

    public static String getDayofWeekSimpleName(String date) {
        return getDayofWeekSimpleName(DateUtil.string2Date(date, yyyy_MM_dd));
    }

    /**
     * 返回自 1970 年 1 月 1 日 00:00:00 GMT 以来此 Date 对象表示的毫秒数
     *
     * @return
     */
    public static String getTimeNum() {
        return new Long(new Date().getTime()).toString();
    }

    /**
     * 取两个日期之间的所有日期
     *
     * @param startStr 开始日期
     * @param endStr   结束日期
     * @return
     * @throws ParseException
     */
    /*public static List<Date> getBetweenDate(String startStr, String endStr) throws ParseException {
       List<Date> list = new ArrayList<Date>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        GregorianCalendar gc1 = new GregorianCalendar(), gc2 = new GregorianCalendar();
        gc1.setTime(sdf.parse(startStr));
        gc2.setTime(sdf.parse(endStr));
        do {
            GregorianCalendar gc3 = (GregorianCalendar) gc1.clone();
            String DateStr = gc3.get(Calendar.YEAR) + "-" + (gc3.get(Calendar.MONTH) + 1) + "- " + gc3.get(Calendar
                    .DAY_OF_MONTH);
            list.add(getDate(DateStr));
            gc1.add(Calendar.DAY_OF_MONTH, 1);
        } while (!gc1.after(gc2));
        return list;
    }*/

    /**
     * 收集起始时间到结束时间之间所有的时间并以字符串集合方式返回
     *
     * @param timeStart
     * @param timeEnd
     * @return
     */
    public static List<String> collectLocalDates(String timeStart, String timeEnd) {
        return collectLocalDates(LocalDate.parse(timeStart), LocalDate.parse(timeEnd));
    }

    /**
     * 收集起始时间到结束时间之间所有的时间并以字符串集合方式返回
     *
     * @param start
     * @param end
     * @return
     */
    public static List<String> collectLocalDates(LocalDate start, LocalDate end) {

        // 用起始时间作为流的源头，按照每次加一天的方式创建一个无限流
        return Stream.iterate(start, localDate -> localDate.plusDays(1))
                // 截断无限流，长度为起始时间和结束时间的差+1个
                .limit(ChronoUnit.DAYS.between(start, end) + 1)
                // 由于最后要的是字符串，所以map转换一下
                .map(LocalDate::toString)
                // 把流收集为List
                .collect(Collectors.toList());
    }


    /**
     * 为日期添加月份
     *
     * @param day
     * @param month
     * @return
     */
    public static Date addMonth(Date day, int month) {
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(day);
        calendar.add(Calendar.MONTH, month);
        return calendar.getTime();
    }

    /**
     * 将日期增加天数
     *
     * @param date
     * @param dayNum
     * @return
     */
    public static Date dateAddDay(Date date, int dayNum) {
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.add(calendar.DATE, dayNum);//把日期往后增加一天.整数往后推,负数往前移动
        date = calendar.getTime();   //这个时间就是日期往后推一天的结果
        return date;
    }

    /**
     * 将日期减天数
     *
     * @param date
     * @param dayNum
     * @return
     */
    public static Date dateDecDay(Date date, int dayNum) {
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.add(calendar.DATE, -dayNum);//把日期往后增加一天.整数往后推,负数往前移动
        date = calendar.getTime();   //这个时间就是日期往后推一天的结果
        return date;
    }

    /**
     * 取两个时间之间的豪秒数
     *
     * @return
     */
    public static Long getBetweenMillis(Date startTime, Date endTime) {
        Long i = endTime.getTime() - startTime.getTime();
        return i;
    }

    public static String getNearHoliday() {
        String[] gljr = {"01-01", "02-14", "03-08", "05-01", "06-01", "09-10", "12-25"};//
        String[] gljrms = {"元旦", "情人节", "妇女节", "劳动节", "儿童节", "教师节", "圣诞节"};
        String[] yljr = {"01-01", "01-15", "05-05", "07-07", "08-15", "09-09", "12-30"};
        String[] yljrms = {"春节", "元宵节", "端午节", "情人节", "中秋节", "重阳节", "除夕"};
        Calendar cal = Calendar.getInstance();
        Date date = cal.getTime();
        //获取最近的阳历节日
        long min_gl = 365;
        int min_gl_index = 0;
        for (int i = 0; i < gljr.length; i++) {
            String jr = gljr[i];
            jr = cal.get(Calendar.YEAR) + "-" + jr;
            Date jrDate = getDate(jr);
            System.out.println(jr);
            long l = getOverDayNum(date, jrDate);
            if (l < min_gl && l > 0) {
                min_gl = l;
                min_gl_index = i;
            }
        }
        //获取最近的农历节日
        long min_yl = 365;
        int min_yl_index = 0;
        for (int i = 0; i < yljr.length; i++) {
            String jr = yljr[i];
            jr = cal.get(Calendar.YEAR) + "-" + jr;
            System.out.println(jr);
            Date jrDate = LunarSolarConverter.LunarToSolarDate(jr);
            long l = getOverDayNum(date, jrDate);
            if (l < min_yl && l > 0) {
                min_yl = l;
                min_yl_index = i;
            }
        }
        if (min_gl < min_yl) {
            return "离" + gljrms[min_gl_index] + "还有" + min_gl + "天";
        } else {
            return "离" + yljrms[min_yl_index] + "还有" + min_yl + "天";
        }

    }

    /**
     * 获取时间段内，月份的起始时间
     * 一月起始时间：上月28-本月28
     *
     * @param startTime
     * @param endTime
     * @return
     */
    public static List<HashMap<String, Date>> getBetweenMonthTime(Date startTime, Date endTime) {
        List<HashMap<String, Date>> list = new ArrayList<HashMap<String, Date>>();
        int betweenMonth = getBetweenMonths(startTime, endTime);

        Calendar startCal = Calendar.getInstance();
        Calendar endCal = Calendar.getInstance();
        Date startMonthDate = null;
        Date endMonthDate = null;
        if (startTime.getTime() == endTime.getTime()) {
            startCal.setTime(startTime);
            endCal.setTime(startTime);
            startCal.add(Calendar.MONTH, -1);
            startCal.set(Calendar.DAY_OF_MONTH, 28);
            endCal.set(Calendar.DAY_OF_MONTH, 28);
            startMonthDate = startCal.getTime();
            endMonthDate = endCal.getTime();
            HashMap<String, Date> map = new HashMap<String, Date>();
            map.put("start_time", startMonthDate);
            map.put("end_time", endMonthDate);
            list.add(map);
            return list;
        }
        for (int i = 0; i < betweenMonth + 1; i++) {
            HashMap<String, Date> map = new HashMap<String, Date>();
            if (i == 0) {
                startCal.setTime(startTime);
                endCal.setTime(startTime);
                endCal.set(Calendar.DAY_OF_MONTH, 28);
                startMonthDate = startCal.getTime();
                if (startMonthDate.getTime() >= endCal.getTime().getTime()) {
                    endCal.add(Calendar.MONTH, 1);
                }
                endMonthDate = endCal.getTime();
                map.put("start_time", startMonthDate);
                map.put("end_time", endMonthDate);
                list.add(map);
            } else {
                startCal.setTime(endMonthDate);
                endCal.setTime(endMonthDate);
                endCal.set(Calendar.DAY_OF_MONTH, 28);
                endCal.add(Calendar.MONTH, 1);
                startMonthDate = startCal.getTime();
                endMonthDate = endCal.getTime();
                map.put("start_time", startMonthDate);
                map.put("end_time", endMonthDate);
                list.add(map);
            }
        }
        if (endMonthDate.getTime() < endTime.getTime()) {
            HashMap<String, Date> map = new HashMap<String, Date>();
            map.put("start_time", endMonthDate);
            map.put("end_time", endTime);
            list.add(map);
        }
        return list;
    }

    public static void main(String[] args) {
        String starTime = "2018-02-15";
        String endTime = "2018-02-21";
      /*String starTime="2018-02-15";
      String endTime="2015-02-21";*/

        collectLocalDates(starTime, endTime).forEach(System.out::println);
        //List<String> betweenDate = getBetweenDate("2018-02-15", "2015-02-21");
    }


    /**
     * @param mss
     * @return 该毫秒数转换为 * days * hours * minutes * seconds 后的格式
     * @author fy.zhang
     */
    public static String formatDuring(long mss) {
        long days = mss / (1000 * 60 * 60 * 24);
        long hours = (mss % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);
        long minutes = (mss % (1000 * 60 * 60)) / (1000 * 60);
        long seconds = (mss % (1000 * 60)) / 1000;
        return hours + " 小时 " + minutes + " 分钟 ";
    }

    /**
     * 获取两个日期之间的日期
     *
     * @param start 开始日期
     * @param end   结束日期
     * @return 日期集合
     */
    public static List<Date> getBetweenDates(Date start, Date end) {
        List<Date> result = new ArrayList<>();
        Calendar tempStart = Calendar.getInstance();
        tempStart.setTime(start);
        tempStart.add(Calendar.DAY_OF_YEAR, 1);
        Calendar tempEnd = Calendar.getInstance();
        tempEnd.setTime(end);
        result.add(start);

        while (tempStart.before(tempEnd)) {
            result.add(tempStart.getTime());
            tempStart.add(Calendar.DAY_OF_YEAR, 1);
        }
        if (!start.equals(end)) {
            result.add(end);
        }
        return result;
    }



    public static List<String> getBetweenDates(String start, String end) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        List<String> dates = new ArrayList<>();
        for (Date date : getBetweenDates(simpleDateFormat.parse(start), simpleDateFormat.parse(end))) {
            dates.add(simpleDateFormat.format(date));
        }
        return dates;
    }

    /**
     * @param begin 时间段的开始
     * @param end   时间段的结束
     * @return 输入的两个Date类型数据之间的时间间格用* days * hours * minutes * seconds的格式展示
     * @author fy.zhang
     */
    public static String formatDuring(Date begin, Date end) {
        return formatDuring(end.getTime() - begin.getTime());
    }


    public static Date nextMonthFirstDate() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.add(Calendar.MONTH, 1);
        return calendar.getTime();
    }

    public static Date getNextDay(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, +1);//+1今天的时间加一天
        date = calendar.getTime();
        return date;
    }

    /**
     * 获取某个日期的某一天
     *
     * @param date
     * @return
     */
    public static Date getOneDay(Date date, Integer day) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, day);//+1今天的时间加一天
        date = calendar.getTime();
        return date;
    }

    /**
     * 获取某个日期的某一天
     *
     * @param date
     * @return
     */
    public static Date getOneYear(Date date, Integer year) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.YEAR, year);//+1今天的时间加一天
        date = calendar.getTime();

        return date;
    }

    public static String getNextDay(String date) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        return simpleDateFormat.format(getNextDay(simpleDateFormat.parse(date)));
    }

    /**
     * 获取两个时间之间的月份 包括前后日期月份
     *
     * @param date1
     * @param date2
     * @return
     * @throws ParseException
     */
    public static int getMonthSpace(String date1, String date2) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Calendar c1 = Calendar.getInstance();
            Calendar c2 = Calendar.getInstance();
            c1.setTime(sdf.parse(date1));
            c2.setTime(sdf.parse(date2));

            return Math.abs(c1.get(Calendar.YEAR) * 12 + c1.get(Calendar.MONTH) - c2.get(Calendar.YEAR) * 12 - c2.get
                    (Calendar.MONTH));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0;
    }


    public static String getYearByDate(String date) {
        String[] split = date.split("-");
        return split[0];
    }

    public static Integer getYearByDateReturnInt(String date) {
        return Integer.parseInt(getYearByDate(date));
    }

    public static String getMonthByDate(String date) {
        String[] split = date.split("-");
        return split[1];
    }

    public static Integer getMonthByDateReturnInt(String date) {
        return Integer.parseInt(getMonthByDate(date));
    }

    public static String getDayByDate(String date) {
        String[] split = date.split("-");
        return split[2];
    }

    public static Integer getDayByDateReturnInt(String date) {
        return Integer.parseInt(getDayByDate(date));
    }

    public static String getNextMonthByDate(String date1) {
        Calendar c = Calendar.getInstance();//获得一个日历的实例
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = sdf.parse(date1);//初始日期
        } catch (Exception e) {
            e.printStackTrace();
        }
        c.setTime(date);//设置日历时间
        c.add(Calendar.MONTH, 1);//在日历的月份上增加6个月
        return sdf.format(c.getTime());//得到6个月后的日期
    }

    public static boolean isCurrentDateByYearMonth(Integer year, Integer month) {
        return year == getCurrentYear() && month == getCurrentMonth();
    }

    public static boolean isGtCurrentDateByYearMonth(int year, int month) {
        return year > getCurrentYear() || (year == getCurrentYear() && month > getCurrentMonth());
    }

    public static Date addMins(Date date, int minute) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MINUTE, minute);
        return calendar.getTime();
    }

    public static Map<String, Integer> getYearAndMonthByNextMonth() {
        Map<String, Integer> date = new HashMap<>();
        date.put("year", getPreYear());
        date.put("month", getPreMonth());
        return date;
    }

    public static Integer getPreMonth() {
        Calendar cal = Calendar.getInstance();
        cal.add(cal.MONTH, 1);
        return Integer.parseInt(new SimpleDateFormat("MM").format(cal.getTime()));
    }

    public static Integer getPreYear() {
        Calendar cal = Calendar.getInstance();
        cal.add(cal.MONTH, 1);
        return Integer.parseInt(new SimpleDateFormat("yyyy").format(cal.getTime()));
    }

    public static Map<String, Integer> getLastMonthDate(int year, int month) {

        Map<String, Integer> map = new HashMap<>();

        Date date = null;
        try {
            date = new SimpleDateFormat("yy-MM").parse(year + "-" + month);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.MONTH, -1);

        map.put("year", Integer.parseInt(new SimpleDateFormat("yyyy").format(cal.getTime())));
        map.put("month", Integer.parseInt(new SimpleDateFormat("MM").format(cal.getTime())));
        return map;
    }

    public static Map<String, Integer> getPreMonthDate(int year, int month) {

        Map<String, Integer> map = new HashMap<>();

        Date date = null;
        try {
            date = new SimpleDateFormat("yy-MM").parse(year + "-" + month);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.MONTH, 1);

        map.put("year", Integer.parseInt(new SimpleDateFormat("yyyy").format(cal.getTime())));
        map.put("month", Integer.parseInt(new SimpleDateFormat("MM").format(cal.getTime())));
        return map;
    }



    public Date addOneSecond(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.SECOND, 1);
        return calendar.getTime();
    }

    public static Integer getBetweenDateCount(String startDateStr, String endDateStr) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date startDate = simpleDateFormat.parse(startDateStr);
        Date endDate = simpleDateFormat.parse(endDateStr);
        List<Date> betweenDates = getBetweenDates(startDate, endDate);
        if (startDate.getTime() > endDate.getTime()) {
            return 1 - betweenDates.size();
        }
        return betweenDates.size() - 1;
    }

    /**
     * 获取莫日期之后几天的年月日
     *
     * @param dateStr
     * @param afterIndex
     * @return
     */
    public static String getAfterDate(String dateStr, int afterIndex) {
        // 时间表示格式可以改变，yyyyMMdd需要写例如20160523这种形式的时间
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        // 将字符串的日期转为Date类型，ParsePosition(0)表示从第一个字符开始解析
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(sdf.parse(dateStr, new ParsePosition(0)));
        // add方法中的第二个参数n中，正数表示该日期后n天，负数表示该日期的前n天
        calendar.add(Calendar.DATE, afterIndex);
        return sdf.format(calendar.getTime());
    }

    public static String getAfterHour(String dateStr, int afterIndex) {
        // 时间表示格式可以改变，yyyyMMdd需要写例如20160523这种形式的时间
        SimpleDateFormat sdf = new SimpleDateFormat(yyyy_MM_ddHHmmss);
        // 将字符串的日期转为Date类型，ParsePosition(0)表示从第一个字符开始解析
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(sdf.parse(dateStr, new ParsePosition(0)));
        // add方法中的第二个参数n中，正数表示该日期后n天，负数表示该日期的前n天
        calendar.add(Calendar.HOUR, afterIndex);
        return sdf.format(calendar.getTime());
    }

    public static String getBeforeHour(String dateStr, int beforeIndex) {
        // 时间表示格式可以改变，yyyyMMdd需要写例如20160523这种形式的时间
        SimpleDateFormat sdf = new SimpleDateFormat(yyyy_MM_ddHHmmss);
        // 将字符串的日期转为Date类型，ParsePosition(0)表示从第一个字符开始解析
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(sdf.parse(dateStr, new ParsePosition(0)));
        // add方法中的第二个参数n中，正数表示该日期后n天，负数表示该日期的前n天
        calendar.add(Calendar.HOUR, -beforeIndex);
        return sdf.format(calendar.getTime());
    }


    public static boolean judgePrepStartDateAndEndDate(String targetDate, String startDate, String endDate) throws ParseException {
        List<String> dates = getBetweenDates(startDate, endDate);
        if (dates.contains(targetDate)) {
            return true;
        }
        return false;
    }

    public static int compareTime(String dateOne, String dateTwo) {
        return dateOne.compareTo(dateTwo);
    }


    public static String getLastDate() {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE, -1);
        return new SimpleDateFormat(yyyy_MM_dd).format(cal.getTime());
    }

    public static Integer getYearOfLastDay() {

        return Integer.parseInt(getLastDate().split("-")[0]);
    }

    public static Integer getMonthOfLastDay() {

        return Integer.parseInt(getLastDate().split("-")[1]);
    }

    public static Integer getLastDay() {

        return Integer.parseInt(getLastDate().split("-")[2]);
    }

    public static String getCurrentDayStr() {
        return LocalDate.now().toString();
    }


    public static String getShiFenMiao() {

        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
        Date date = new Date();
        return sdf.format(date).toString();
    }

    /**
     * 根据年 月 获取对应的月份 天数
     */
    public static int getDaysByYearMonth(int year, int month) {
        Calendar a = Calendar.getInstance();
        a.set(Calendar.YEAR, year);
        a.set(Calendar.MONTH, month - 1);
        a.set(Calendar.DATE, 1);
        a.roll(Calendar.DATE, -1);
        int maxDate = a.get(Calendar.DATE);
        return maxDate;
    }


    /**
     * 获取当前日期是星期几<br>
     *
     * @param date
     * @return 当前日期是星期几
     */
    public static String getWeekOfDate(Date date) {
        String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0)
            w = 0;
        return weekDays[w];
    }

    public static String getWeekOfDate(String date) throws ParseException {
        return getWeekOfDate(new SimpleDateFormat("yyyy-MM-dd").parse(date));
    }


    /**
     * 获得该月最后一天
     *
     * @param year
     * @param month
     * @return
     */
    public static String getLastDayOfMonth(int year, int month) {
        Calendar cal = Calendar.getInstance();
        //设置年份
        cal.set(Calendar.YEAR, year);
        //设置月份
        cal.set(Calendar.MONTH, month - 1);
        //获取某月最大天数
        int lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        //设置日历中月份的最大天数
        cal.set(Calendar.DAY_OF_MONTH, lastDay);
        //格式化日期
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String lastDayOfMonth = sdf.format(cal.getTime());
        return lastDayOfMonth;
    }

}


